package engine.systems.render.pen
{
	import engine.classes.enumerations.SystemPriority;
	import engine.groups.Group;
	import engine.systems.update.GameSystem;
	import engine.utils.GeomUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	/**
	 * BROKEN
	 */
	public class PenSystem extends GameSystem
	{
		private var x:int;
		private var y:int;
		private var i:int;
		private var j:int;
		
		public function PenSystem()
		{
			super(PenNode, updateNode);
			
			this.priority = SystemPriority.UPDATE;
		}
		
		private function updateNode(node:PenNode, time:Number):void
		{
			if(node.pen.invalidate)
			{
				node.pen.invalidate = false;
				Bitmap(node.render.display).bitmapData = node.pen.data;
			}
			
			if(node.pen.drawing)
			{
				x = node.render.display.mouseX;
				y = node.render.display.mouseY;
				
				node.pen.data.lock();
				for(i = -node.pen.radius; i <= node.pen.radius; i++)
				{
					for(j = -node.pen.radius; j <= node.pen.radius; j++)
					{
						if(node.pen.circle)
						{
							if(GeomUtils.distance(x, y, x + i, y + j) <= node.pen.radius)
								node.pen.data.setPixel32(x + i, y + j, node.pen.color);
						}
						else node.pen.data.setPixel32(x + i, y + j, node.pen.color);
					}
				}
				node.pen.data.unlock();
			}
			
			if(node.pen.erase)
			{
				node.pen.erase = false;
				node.pen.data.fillRect(node.pen.data.rect, 0);
			}
		}
	}
}